<!--
 * @Descripttion: 海报列表
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-29 14:01:10
-->
<template>
    <div class="lb-poster-list">
        <top-nav/>
        <el-row class="page-main">
          <div class="page-top-operate">
            <lb-button size='medium' type='primary' icon='el-icon-plus' @click='addOrEditPoster("add")'>添加海报</lb-button>
          </div>
          <el-row class="page-search-form">
            <el-form @submit.native.prevent
            :inline="true"
            :model="searchForm"
            ref="searchForm"
            >
              <el-form-item label="海报分类" prop="type_id">
                <el-select v-model="searchForm.type_id" placeholder="请选择">
                  <el-option
                  v-for="(item,index) in posterOptions"
                  :key='index'
                  :label="item.title"
                  :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <lb-button size="medium" type="primary" icon="el-icon-search" style="margin-right:5px" @click="getTableDataList(1)">{{$t('action.search')}}</lb-button>
                <lb-button size="medium" icon="el-icon-refresh-left" style="margin-right:5px" @click="resetForm('searchForm')">{{$t('action.reset')}}</lb-button>
              </el-form-item>
            </el-form>
          </el-row>
          <el-table
            v-loading='loading'
            :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
            style="width: 100%">
              <el-table-column
                prop="id"
                label="ID">
              </el-table-column>
              <el-table-column
                prop="title"
                label="标题">
              </el-table-column>
              <el-table-column
                prop="type_title"
                label="分类">
              </el-table-column>
              <el-table-column
                label="封面图">
                <template slot-scope="scope">
                  <lb-image  :src="scope.row.cover" />
                </template>
              </el-table-column>
              <el-table-column
                prop="top"
                label="排序值">
              </el-table-column>
              <el-table-column
                prop="create_time"
                label="加入时间">
                  <template slot-scope="scope">
                      <p>{{scope.row.create_time | handleTime(1)}}</p>
                      <p>{{scope.row.create_time | handleTime(2)}}</p>
                  </template>
              </el-table-column>
              <el-table-column
                prop="status"
                label="是否上架">
                  <template slot-scope="scope">
                    <el-switch
                    v-model="scope.row.status"
                    :active-value="1"
                    :inactive-value="0"
                    @change="updatePoster(scope.row.id,scope.row.status)"
                    ></el-switch>
                  </template>
              </el-table-column>
              <el-table-column
                prop="status"
                label="操作">
                  <template slot-scope="scope">
                    <div class="table-operate">
                      <lb-button type='primary' size='mini' plain @click="addOrEditPoster('edit', scope.row.id)">{{$t('action.edit')}}</lb-button>
                      <lb-button type='danger' size='mini' plain @click="delPoster(scope.row.id)">{{$t('action.delete')}}</lb-button>
                    </div>
                  </template>
              </el-table-column>
          </el-table>
          <lb-page
          :batch='false'
          :page='searchForm.page'
          :pageSize='searchForm.limit'
          :total='total'
          @handleSizeChange='handleSizeChange'
          @handleCurrentChange='handleCurrentChange'
          >
        </lb-page>
        </el-row>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      posterOptions: [],
      tableData: [],
      searchForm: {
        type_id: 0,
        limit: 10,
        page: 1
      },
      total: 0
    }
  },
  created () {
    this.getSelectedOptions()
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    updatePoster (id, status) {
      let obj = {id, status}
      this.$api.updatePoster(obj).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status === -1) {
            this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
            this.getTableDataList()
          }
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    },
    getSelectedOptions () {
      this.$api.getPosterSelectedOptions().then(res => {
        if (res.code === 200) {
          this.posterOptions = res.data.data
          this.posterOptions.unshift({
            title: '全部',
            id: 0
          },
          {
            title: '暂无分类',
            id: -1
          })
        }
      })
    },
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let {searchForm} = this
      searchForm.page_count = searchForm.limit
      this.$api.getPosterList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.total = res.data.total
          this.tableData = res.data.data
        }
      })
    },
    addOrEditPoster (type, id) {
      let url = type === 'add' ? `/app/poster/edit` : `/app/poster/edit?id=${id}`
      this.$router.push(url)
    },
    /**
     * @method 删除海报
     */
    delPoster (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updatePoster(id, -1)
      })
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
  .lb-poster-list{
    width: 100%;
    .el-input,
    .el-select{
      width: 200px;
    }
    .page-main{
      width: 100%;
      .el-table{
        img{
          width: 60px;
          height: 60px;
        }
      }
    }
  }
</style>
